<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <payaccountSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true"  show-summary :summary-method="getSummaries"  :cell-style="cellStyle"  @done="onDone"  :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <!-- <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit({},'1')"
            >
              添加
            </el-button> -->
            <el-button
            v-permission="'Financial_Payaccount_Export'"
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
          </template>
           <!-- 标题列 -->
           <template slot="addAmountCn" slot-scope="{ row }">
            ￥{{ row.addAmountCn }}
          </template>
           <!-- 标题列 -->
           <template slot="reduceAmountCn" slot-scope="{ row }">
            ￥{{ row.reduceAmountCn }}
          </template>
          <template slot="actualAmountCn" slot-scope="{ row }">
            ￥{{ row.actualAmountCn }}
          </template>
           <!-- 操作列 -->
         <template slot="action" slot-scope="{ row }"  >
          <el-link type="info"  :underline="false"  icon="el-icon-menu" @click="openEdit(row,'3')" >明细</el-link>
        </template>
        </ele-pro-table>
      </el-card>
       <!-- 编辑弹窗 -->
       <payaccountDetail :data="current" :visible.sync="showEdit" @done="reload" :btnIndex="btnIndex"/>
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    
    import { removePageTab } from '@/utils/page-tab-util';
    import ExportDialog from '@/components/Excel/ExportDialog'
    import payaccountDetail from './components/payaccount-detail.vue';
    import payaccountSearch from './components/payaccount-search.vue';
    import { QueryPayaccountByCondition } from '@/api/finance/payaccount';

    export default {
      name: 'Payaccount',
      components: {payaccountDetail,payaccountSearch,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {prop: 'supplierDesc', label: '供应商', showOverflowTooltip: true,align: 'center',},
            {prop: 'currencyDesc', label: '币种', showOverflowTooltip: true,align: 'center',},
            {prop: 'addAmount', label: '应付', showOverflowTooltip: true, align: 'center',sortable:'addAmount' },
            {prop: 'reduceAmount', label: '已付', showOverflowTooltip: true, align: 'center',sortable:'reduceAmount'},
            {prop: 'actualAmount', label: '剩余未付', showOverflowTooltip: true, align: 'center',sortable:'actualAmount'},
            {prop: 'addAmountCn', label: '应付(￥)', showOverflowTooltip: true, align: 'center',sortable:'addAmountCn'},
            {prop: 'reduceAmountCn', label: '已付(￥)', showOverflowTooltip: true, align: 'center',sortable:'reduceAmountCn'},
            {prop: 'actualAmountCn', label: '剩余未付(￥)', showOverflowTooltip: true, align: 'center',sortable:'actualAmountCn'},
            {columnKey: 'action', label: '操作', width: 80, align: 'center', resizable: false,slot: 'action', hideInSetting: true, showOverflowTooltip: true}
          ],
          //是否详情
          btnIndex: '1',
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,
          showCostEdit:false,

          addAmountTotal:0,
          reduceAmountTotal:0,
          actualAmountTotal:0,
          addAmountTotalCn:0,
          reduceAmountTotalCn:0,
          actualAmountCn:0,
          // 导出列
          exportList: [
            { name: "供应商", keys: "supplierDesc", status: true },
            { name: "币种", keys: "currencyDesc", status: true },
            { name: "应付", keys: "addAmount", status: true },
            { name: "已付", keys: "reduceAmount", status: true },
            { name: "剩余未付", keys: "actualAmount", status: true },
            { name: "应付(￥)", keys: "addAmountCn", status: true },
            { name: "已付(￥)", keys: "reduceAmountCn", status: true },
            { name: "剩余未付(￥)", keys: "actualAmountCn", status: true },
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }
        
          let data = await QueryPayaccountByCondition(params.where);

          this.actualAmountTotal = data.actualAmountTotal;
          this.addAmountTotal = data.addAmountTotal;
          this.reduceAmountTotal = data.reduceAmountTotal;
          this.actualAmountCn = data.actualAmountCn;
          this.addAmountTotalCn = data.addAmountTotalCn;
          this.reduceAmountTotalCn = data.reduceAmountTotalCn;

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          
          return {
              list:data.payAccountViews,
              count:data.totalrecords,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },
        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row,index) {
          removePageTab({ key: '/financialmanager/payaccountmanager/payaccountdetail' });
          this.$nextTick(() => {
            this.$router.push({path: '/financialmanager/payaccountmanager/payaccountdetail', query: { "supplierId": row.supplierId,"currencyId":row.currencyId} });
          });
        },

        /* 删除 */
        remove(row) {
          const loading = this.$loading({ lock: true });
          DeleteBankaccount(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        },
        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: [],
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QueryPayaccountByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "应付实时账列表"
              this.$refs.ExportDialog.dataList = data.payAccountViews;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },

        /* 合计 */
        getSummaries(param){
          const sums = [];
          sums[0] = '合计' ;
          const { columns, data } = param;
          columns.forEach((column, index) => {
            if(index ==3){
              sums[index] =  Number(this.addAmountTotal).toFixed(2);
            }else if(index ==4){
              sums[index] =  Number(this.reduceAmountTotal).toFixed(2);
            }else if(index ==5){
              sums[index] =  Number(this.actualAmountTotal).toFixed(2);
            }else if(index ==6){
              sums[index] = Number(this.addAmountTotalCn).toFixed(2);
            }else if(index ==7){
              sums[index] = Number(this.reduceAmountTotalCn).toFixed(2);
            }else if(index ==8){
              sums[index] = Number(this.actualAmountTotal).toFixed(2);
            }
          });

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          return sums;
        },

        /* 样式 */
        cellStyle(param){
          const { row, column, rowIndex, columnIndex } = param;
          if(columnIndex==3||columnIndex==4||columnIndex==5||columnIndex==6||columnIndex==7||columnIndex==8){
            return{
              color:''
            }
          }
        },
      }
    };
  </script>
   <style lang="scss" scoped>
    /*总计字体颜色
    ::v-deep .el-table__footer-wrapper tbody td {
      color: var(--color-primary);
      cursor: pointer;
    }*/
  </style>